#### R script for PEC appendix code
#### Jinhyuk, 04/11/2021
###########################################

###############
### Setup R ###
###############

### Clear terminal
cat("\014")

### Clear space
rm(list = ls())

### library packages

install.packages('rio')
library(rio)
install.packages('dplyr')
library(dplyr)
install.packages('mlr')
library(mlr)
install.packages('lattice')
library(lattice)
install.packages('ggplot2')
library(ggplot2)
install.packages('ggthemes')
library(ggthemes)
install.packages('gridExtra')
library(gridExtra)
install.packages('sandwich')
library(sandwich)
install.packages('lmtest')
library(lmtest)
install.packages('lmPerm')
library(lmPerm)
install.packages('coefplot')
library(coefplot)
install.packages('stargazer')
library(stargazer)
install.packages('hrbrthemes')
library(hrbrthemes)
install.packages('officer')
library(officer)
install.packages('flextable')
library(flextable)
install.packages('meta')
library(meta)
install.packages('grep')
library(grep)
install.packages('MASS')
library(MASS)

### Load custom functions
#### Multiplot function
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
# - cols:   Number of columns in layout
# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
#
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
# then plot 1 will go in the upper left, 2 will go in the upper right, and
# 3 will go all the way across the bottom.
#
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  library(grid)
  
  # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)
  
  numPlots = length(plots)
  
  # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
    # Make the panel
    # ncol: Number of columns of plots
    # nrow: Number of rows needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                     ncol = cols, nrow = ceiling(numPlots/cols))
  }
  
  if (numPlots==1) {
    print(plots[[1]])
    
  } else {
    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
    
    # Make each plot, in the correct location
    for (i in 1:numPlots) {
      # Get the i,j matrix positions of the regions that contain this subplot
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
      
      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                      layout.pos.col = matchidx$col))
    }
  }
}

### Load data
df <- read.csv("df.csv")
df2 <- read.csv("df2.csv")

#### Table 1: The impact of coalition treatment on voters' perception on the ideological position of center party (Komeito Treatment)

### Experiment 1

## Model 1: Komeito with socio-demographic covariates (DV=Ideological Placement)

mod.1 <- lm(komeito_placement ~ one_noc + one_llc + one_rlc_1 + one_rlc_2 + one_rlc_3 + female + as.numeric(Q2.1) + edu + as.numeric(Q13_1) + income, data = df)
summary(mod.1)

stargazer(mod.1)

## Model 2: Komeito without socio-demographic covariates (DV=Ideological Placement)

mod <- lm(komeito_placement ~ one_noc + one_llc + one_rlc_1 + one_rlc_2 + one_rlc_3, data = df)
summary(mod)

stargazer(mod)

#### Table 2: The impact of coalition treatment on voters' perception on the ideological position of center party (DPFP Treatment)

### Experiment 2

## Model 3: DPFP with socio-demographic covariates (DV=Ideological Placement)

mod2.1 <- lm(dpfp_placement ~ two_noc + two_llc + two_rlc + female + as.numeric(Q2.2) + edu + as.numeric(Q13_1) + income, data = df2)
summary(mod2.1)

stargazer(mod2.1)

## Model 4: DPFP without socio-demographic covariates (DV=Ideological Placement)  

mod2 <- lm(dpfp_placement ~ two_noc + two_llc + two_rlc, data = df2)
summary(mod2)

stargazer(mod2)

###########################################################################################

#### Table 3: The impact of coalition treatment on voters' expectations on the likelihood of the formation of a coalition (Komeito Treatment)

### Experiment 1

## Model 1: Komeito with socio-demographic covariates (DV: Likelihood of formation of no coalition)

mod3.1 <- lm(likelinc~ one_noc + one_llc + one_rlc_1 + one_rlc_2 + one_rlc_3 + female + as.numeric(Q2.1) + edu + as.numeric(Q13_1) + income, df)
summary(mod3.1)

stargazer(mod3.1)

## Model 2: Komeito without socio-demographic covariates (DV: Likelihood of formation of no coalition)

mod3 <- lm(likelinc~ one_noc + one_llc + one_rlc_1 + one_rlc_2 + one_rlc_3, df)
summary(mod3)

stargazer(mod3)

## Model 3: Komeito with socio-demographic covariates (DV: Likelihood of formation of right-leaning coalition (LDP-Komeito-Nippon Ishin))

mod4.1 <- lm(likelilkn~ one_noc + one_llc + one_rlc_1 + one_rlc_2 + one_rlc_3 + female + as.numeric(Q2.1) + edu + as.numeric(Q13_1) + income, df)
summary(mod4.1)

stargazer(mod4.1)

## Model 4: Komeito without socio-demographic covariates (DV: Likelihood of formation of right-leaning coalition (LDP-Komeito-Nippon Ishin))

mod4 <- lm(likelilkn~ one_noc + one_llc + one_rlc_1 + one_rlc_2 + one_rlc_3, df)
summary(mod4)

stargazer(mod4)

## Model 5: Komeito with socio-demographic covariates (DV: Komeito-DPFP-CDPJ-SDPJ-JCP)

mod6.1 <- lm(likelikdcsj~ one_noc + one_llc + one_rlc_1 + one_rlc_2 + one_rlc_3 + female + as.numeric(Q2.1) + edu + as.numeric(Q13_1) + income, df)
summary(mod6.1)

stargazer(mod6.1)

## Model 6: Komeito without socio-demographic covariates (DV: Komeito-DPFP-CDPJ-SDPJ-JCP)

mod6 <- lm(likelikdcsj~ one_noc + one_llc + one_rlc_1 + one_rlc_2 + one_rlc_3, df)
summary(mod6)

stargazer(mod6)

#### Table 4. The impact of coalition treatment on voters' expectations on the likelihood of the formation of a coalition (DPFP Treatment)

### Experiment 2

## Model 1: DPFP with socio-demographic covariates (DV: Likelihood of formation of no coalition)

mod8.1 <- lm(likelinc~ two_noc + two_llc + two_rlc + female + as.numeric(Q2.2) + edu + as.numeric(Q13_1) + income, df2)
summary(mod8.1)

stargazer(mod8.1)

## Model 2: DPFP without socio-demographic covariates (DV: Likelihood of formation of no coalition)

mod8 <- lm(likelinc~ two_noc + two_llc + two_rlc, df2)
summary(mod8)

stargazer(mod8)

## Model 3: DPFP with socio-demographic covariates (DV: Likelihood of formation of right-leaning coalition (DPFP-Komeito-Nippon Ishin))

mod10.1 <- lm(likelidkn~ two_noc + two_llc + two_rlc + female + as.numeric(Q2.2) + edu + as.numeric(Q13_1) + income, df2)
summary(mod10.1)

stargazer(mod10.1)

## Model 4: DPFP without socio-demographic covariates (DV: Likelihood of formation of right-leaning coalition (DPFP-Komeito-Nippon Ishin))

mod10 <- lm(likelidkn~ two_noc + two_llc + two_rlc, df2)
summary(mod10)

stargazer(mod10)

## Model 5: DPFP with socio-demographic covariates (DPFP-CDPJ-SDPJ-JCP)

mod12.1 <- lm(likelidcsj~ two_noc + two_llc + two_rlc + female + as.numeric(Q2.2) + edu + as.numeric(Q13_1) + income, df2)
summary(mod12.1)

stargazer(mod12.1)

## Model 6: DPFP without socio-demographic covariates (DV: Likelihood of formation of left-leaning coalition (DPFP-CDPJ-SDPJ-JCP))

mod12 <- lm(likelidcsj~ two_noc + two_llc + two_rlc, df2)
summary(mod12)

stargazer(mod12)

